package com.service.impl;

import com.alibaba.fastjson.JSON;
import com.dao.RoomTypeDao;
import com.pojo.RoomType;
import com.service.RoomTypeService;
import com.util.ConstUtils;
import com.util.JedisPoolUtils;
import com.util.RedisKey;
import com.vo.RoomTypeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;

import java.io.File;
import java.util.List;

/**
 * @author mornd
 * @date 2020/11/24 - 11:01
 */
@Service
@Transactional
public class RoomTypeServiceImpl implements RoomTypeService {
    @Autowired
    private RoomTypeDao roomTypeDao;

    /**
     * 根据条件查询房型
     * @param roomTypeVo
     * @return
     */
    public List<RoomType> findRoomTypeList(RoomTypeVo roomTypeVo) {
        return roomTypeDao.findRoomTypeList(roomTypeVo);
    }

    /**
     * 添加房型
     * @param roomType
     * @return
     */
    public int addRoomType(RoomType roomType) {
        roomType.setReservedNum(0);//房间已预定数量
        roomType.setAvilableNum(roomType.getRoomNum());//可住房间数
        roomType.setLivedNum(0);//已入住数量
        int count = roomTypeDao.addRoomType(roomType);
        //清空redis缓存数据
        if (count > 0){
            JedisPoolUtils.getJedis().del(RedisKey.ROOM_TYPE_LIST);
            findRoomTypeByRedis();
        }
        return count;
    }

    /**
     * 修改房型
     * @param roomType
     * @return
     */
    public int updateRoomType(RoomType roomType) {
        //根据id获取房型详情
        RoomType roomTypeById = findRoomTypeById(roomType.getId());
        //如果数据库中的图片地址不等于上传的地址 则说明用户修改了图片 则删除之前的图片
        if(!roomTypeById.getPhoto().equals(roomType.getPhoto())){
            File updateImage = new File(ConstUtils.ROOMTYPE_UPLOAD_PATH + roomTypeById.getPhoto());
            if(updateImage.exists()){
                //父文件夹对象
                File delParent = new File(String.valueOf(updateImage.getParentFile()));
                //删除老图片
                updateImage.delete();
                //删除图片后 如果该图片所属的文件夹为空 则删除该文件夹
                if(delParent.list().length == 0){
                    delParent.delete();
                }
            }
        }
        roomType.setAvilableNum(roomType.getRoomNum());//可住房间数
        int count = roomTypeDao.updateRoomType(roomType);
        //清空redis缓存数据
        if (count > 0){
            JedisPoolUtils.getJedis().del(RedisKey.ROOM_TYPE_LIST);
            findRoomTypeByRedis();
        }
        return count;
    }

    /**
     * 根据房型id查询房间数量
     * @param roomTypeId
     * @return
     */
    public int getRoomCount(Integer roomTypeId) {
        return roomTypeDao.getRoomCount(roomTypeId);
    }

    /**
     * 删除房型
     * @param roomTypeId
     * @param deleteImagePath
     * @return
     */
    public boolean deleteRoomType(Integer roomTypeId,String deleteImagePath) {
        try {
            //如果不是默认图片则删除
            if(!ConstUtils.ROOMTYPE_DEFAULT_IMAGE_NAME.equals(deleteImagePath)){
                //删除的图片对象
                File delImage = new File(ConstUtils.ROOMTYPE_UPLOAD_PATH + deleteImagePath);
                //删除该房型对应的图片
                if(delImage.exists()){
                    //父文件夹对象
                    File delParent = new File(String.valueOf(delImage.getParentFile()));
                    delImage.delete();
                    //删除图片后 如果该图片所属的文件夹为空 则删除该文件夹
                    if(delParent.list().length == 0){
                        delParent.delete();
                    }
                }
            }
            //删除数据库数据
            roomTypeDao.deleteRoomType(roomTypeId);
            //清空redis缓存数据
            JedisPoolUtils.getJedis().del(RedisKey.ROOM_TYPE_LIST);
            findRoomTypeByRedis();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 将房型集合添加至redis缓存中
     * @return
     */
    public String findRoomTypeByRedis(){
        //创建jedis对象
        Jedis jedis = JedisPoolUtils.getJedis();
        //拿取房型集合的键
        String roomTypeList = jedis.get(RedisKey.ROOM_TYPE_LIST);
        //判断是否为空
        if(StringUtils.isEmpty(roomTypeList)){
            //从数据库中查询数据
            List<RoomType> roomTypes = findRoomTypeList(null);
            //将数据添加至缓存中
            roomTypeList = jedis.set(RedisKey.ROOM_TYPE_LIST, JSON.toJSONString(roomTypes));
        }
        return roomTypeList;
    }

    /**
     * 根据房型id查询详情
     * @param id
     * @return
     */
    @Override
    public RoomType findRoomTypeById(Integer id) {
        return roomTypeDao.findRoomTypeById(id);
    }
}
